package com.example.shixunqq;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.shixunqq.databinding.ActivityGuideBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {
    private ActivityGuideBinding binding;
    private List<GuideItem> guideItems;
    private View[] indicators;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityGuideBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 检查是否是首次启动
        SharedPreferences prefs = getSharedPreferences("app_settings", MODE_PRIVATE);
        boolean isFirstLaunch = prefs.getBoolean("is_first_launch", true);

        if (!isFirstLaunch) {
            startMainActivity();
            return;
        }

        initGuideItems();
        setupViewPager();
        setupIndicators();
        
        // 设置按钮点击事件
        binding.startButton.setOnClickListener(v -> {
            // 保存首次启动状态
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("is_first_launch", false);
            editor.apply();
            
            startMainActivity();
        });
    }

    private void initGuideItems() {
        guideItems = new ArrayList<>();
        guideItems.add(new GuideItem(R.drawable.guide_1, "实时天气预报\n随时掌握天气变化"));
        guideItems.add(new GuideItem(R.drawable.guide_2, "精准定位服务\n获取本地天气信息"));
        guideItems.add(new GuideItem(R.drawable.guide_3, "天气走势分析\n掌握未来天气趋势"));
        guideItems.add(new GuideItem(R.drawable.guide_4, "生活指数提醒\n合理安排日常活动"));
    }

    private void setupViewPager() {
        GuideAdapter adapter = new GuideAdapter(guideItems);
        binding.viewPager.setAdapter(adapter);
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateIndicators(position);
                binding.startButton.setVisibility(position == guideItems.size() - 1 ? View.VISIBLE : View.GONE);
            }
        });
    }

    private void setupIndicators() {
        indicators = new View[guideItems.size()];
        for (int i = 0; i < indicators.length; i++) {
            indicators[i] = new View(this);
            int size = (int) (getResources().getDisplayMetrics().density * 8);
            LayoutParams params = new LayoutParams(size, size);
            params.setMargins(8, 0, 8, 0);
            indicators[i].setLayoutParams(params);
            indicators[i].setBackgroundResource(R.drawable.indicator_inactive);
            binding.indicatorLayout.addView(indicators[i]);
        }
        updateIndicators(0);
    }

    private void updateIndicators(int position) {
        for (int i = 0; i < indicators.length; i++) {
            indicators[i].setBackgroundResource(i == position ? R.drawable.indicator_active : R.drawable.indicator_inactive);
        }
    }

    private void startMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    private static class GuideItem {
        int imageRes;
        String text;

        GuideItem(int imageRes, String text) {
            this.imageRes = imageRes;
            this.text = text;
        }
    }

    private static class GuideAdapter extends RecyclerView.Adapter<GuideAdapter.GuideViewHolder> {
        private final List<GuideItem> items;

        GuideAdapter(List<GuideItem> items) {
            this.items = items;
        }

      @NotNull
        @Override
        public GuideViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_guide, parent, false);
            return new GuideViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NotNull GuideViewHolder holder, int position) {
            GuideItem item = items.get(position);
            holder.imageView.setImageResource(item.imageRes);
            holder.textView.setText(item.text);
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        static class GuideViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView textView;

            GuideViewHolder(@NotNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.guideImage);
                textView = itemView.findViewById(R.id.guideText);
            }
        }
    }
} 